home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Graphic Gems I, II & III (C_C++) / Graphics Gems C Code.sea / GemsIII / alloc / alloc.c next >
Text File  |  1992-06-16  |  4KB  |  193 lines

  1. /* alloc.c
  2.  *
  3.  * A simple fast memory allocation package.
  4.  *
  5.  * AllocInit()    - create an alloc pool, returns the old pool handle.
  6.  * Alloc()        - allocate memory.
  7.  * AllocReset()   - reset the current pool.
  8.  * AllocSetPool() - set the current pool.
  9.  * AllocFree()    - free the memory used by the current pool.
  10.  *
  11.  */
  12.  
  13. #include <stdio.h>
  14.  
  15. #include "alloc.h"
  16.  
  17. /* ALLOC_BLOCK_SIZE - adjust this size to suit your installation - it should
  18.  * be reasonably * large otherwise you will be mallocing a lot.
  19.  */
  20.  
  21. #define ALLOC_BLOCK_SIZE        (100*1024)
  22.  
  23. /* alloc_hdr_t - Header for each block of memory
  24.  */
  25.  
  26. typedef
  27. struct alloc_hdr_s
  28. {
  29.         struct alloc_hdr_s *next;   /* Next Block          */
  30.         char               *block,  /* Start of block      */
  31.                            *free,   /* Next free in block  */
  32.                            *end;    /* block + block size  */
  33. }
  34. alloc_hdr_t;
  35.  
  36. /* alloc_root_t - Header for the whole pool
  37.  */
  38.  
  39. typedef
  40. struct alloc_root_s
  41. {
  42.         alloc_hdr_t *first,   /* First header in pool */
  43.                     *current; /* Current header       */
  44. }
  45. alloc_root_t;
  46.  
  47. /* root - Pointer to the the current pool
  48.  */
  49.  
  50. static alloc_root_t *root;
  51.  
  52. /* AllocHdr()
  53.  *
  54.  * Private routine to allocate a header and memory block.
  55.  */
  56.  
  57. static
  58. alloc_hdr_t *
  59. AllocHdr()
  60. {
  61.         alloc_hdr_t        *hdr;
  62.         char               *block;
  63.  
  64.         block = (char *) malloc(ALLOC_BLOCK_SIZE);
  65.         hdr   = (alloc_hdr_t *) malloc(sizeof(alloc_hdr_t));
  66.  
  67.         if (hdr == NULL || block == NULL)
  68.         {
  69.                 fprintf(stderr, "Out of memory\n");
  70.                 exit(1);
  71.         }
  72.         hdr->block = block;
  73.         hdr->free  = block;
  74.         hdr->next  = NULL;
  75.         hdr->end   = block + ALLOC_BLOCK_SIZE;
  76.  
  77.         return(hdr);
  78. }
  79.  
  80. /* AllocInit()
  81.  *
  82.  * Create a new memory pool with one block.
  83.  * Returns pointer to the previous pool.
  84.  */
  85.  
  86. alloc_handle_t *
  87. AllocInit()
  88. {
  89.         alloc_handle_t        *old = (alloc_handle_t *) root;
  90.  
  91.         root = (alloc_root_t *) malloc(sizeof(alloc_root_t));
  92.         root->first = AllocHdr();
  93.         root->current  = root->first;
  94.         return(old);
  95. }
  96.  
  97. /* Alloc()
  98.  *
  99.  * Use as a direct replacement for malloc().  Allocates memory
  100.  * from the current pool.
  101.  */
  102.  
  103. char *
  104. Alloc(size)
  105. int        size;
  106. {
  107.         alloc_hdr_t        *hdr = root->current;
  108.         char               *ptr;
  109.  
  110.         /* Align to 4 byte boundary - should be OK for most machines.
  111.          * Change this if your machine has wierd alignment requirements
  112.          */
  113.         size = (size + 3) & 0xfffffffc;
  114.  
  115.         ptr = hdr->free;
  116.         hdr->free += size;
  117.  
  118.         /* Check if the current block is exhausted */
  119.  
  120.         if (hdr->free >= hdr->end)
  121.         {
  122.                 /* Is the next block already allocated? */
  123.  
  124.                 if (hdr->next != NULL)
  125.                 {
  126.                         /* re-use block */
  127.                         hdr->next->free = hdr->next->block;
  128.                         root->current = hdr->next;
  129.                 }
  130.                 else
  131.                 {
  132.                         /* extend the pool with a new block */
  133.                         hdr->next = AllocHdr();
  134.                         root->current = hdr->next;
  135.                 }
  136.  
  137.                 /* set ptr to the first location in the next block */
  138.                 ptr = root->current->free;
  139.                 root->current->free += size;
  140.         }
  141.         /* Return pointer to allocated memory */
  142.         return(ptr);
  143. }
  144.  
  145. /* AllocSetPool()
  146.  *
  147.  * Change the current pool.  Return the old pool.
  148.  */
  149.  
  150. alloc_handle_t *
  151. AllocSetPool(new)
  152. alloc_handle_t        *new;
  153. {
  154.         alloc_handle_t *old = (alloc_handle_t *) root;
  155.  
  156.         root = (alloc_root_t *) new;
  157.         return(old);
  158. }
  159.  
  160. /* AllocReset()
  161.  *
  162.  * Reset the current pool for re-use.  No memory is freed, so
  163.  * this is very fast.
  164.  */
  165.  
  166. void
  167. AllocReset()
  168. {
  169.         root->current = root->first;
  170.         root->current->free = root->current->block;
  171. }
  172.  
  173. /* AllocFreePool()
  174.  *
  175.  * Free the memory used by the current pool.
  176.  * Don't use where AllocReset() could be used.
  177.  */
  178.  
  179. void
  180. AllocFreePool()
  181. {
  182.         alloc_hdr_t        *hdr = root->first;
  183.  
  184.         while (hdr != NULL)
  185.         {
  186.                 free((char *) hdr->block);
  187.                 free((char *) hdr);
  188.                 hdr = hdr->next;
  189.         }
  190.         free((char *) root);
  191.         root = NULL;
  192. }
  193.